<template>
  <el-dialog
    v-model="visible"
    :title="`维度使用情况 - ${dimensionCode}`"
    width="700px"
    destroy-on-close
    :before-close="handleBeforeClose"
  >
    <div v-loading="loading" class="usage-container">
      <!-- 概览信息 -->
      <el-card class="usage-section-card">
        <template #header>
          <span class="section-title">概览信息</span>
        </template>
        <el-descriptions :column="1" border>
          <el-descriptions-item label="维度代码">
            <span class="monospace-text">{{ dimensionCode }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="使用次数">
            {{ usageData.total || 0 }} 个模板正在使用
          </el-descriptions-item>
          <el-descriptions-item label="影响范围">
            删除此维度将影响 {{ usageData.total || 0 }} 个模板的评分功能
          </el-descriptions-item>
        </el-descriptions>
      </el-card>

      <!-- 使用详情 -->
      <el-card class="usage-section-card">
        <template #header>
          <span class="section-title">使用详情</span>
        </template>
        <div class="usage-details">
          <div class="detail-title">📋 使用该维度代码的模板列表:</div>

          <div
            v-for="(template, index) in usageData.templates || []"
            :key="index"
            class="template-card"
          >
            <div class="template-header">
              <el-tag
                :type="template.status === '启用中' ? 'success' : 'info'"
                size="small"
                class="status-tag"
              >
                {{ template.status }}
              </el-tag>
              <span class="template-name">
                {{ index + 1 }}. {{ template.name }} ({{ template.code }})
              </span>
              <el-button
                type="text"
                size="small"
                @click="handleGoToTemplate(template.id)"
                class="view-template-btn"
              >
                查看模板
              </el-button>
            </div>

            <div class="template-divider"></div>

            <div class="template-info">
              <div class="info-row">
                <span class="info-label">模板状态:</span>
                <span class="info-value">{{ template.status }}</span>
              </div>
              <div class="info-row">
                <span class="info-label">维度ID:</span>
                <span class="info-value">{{ template.dimension_id }}</span>
              </div>
              <div class="info-row">
                <span class="info-label">维度名称:</span>
                <span class="info-value">{{ template.dimension_name }}</span>
              </div>
              <div class="info-row">
                <span class="info-label">维度状态:</span>
                <span class="info-value">
                  <el-tag
                    :type="
                      template.dimension_status === '启用' ? 'success' : 'info'
                    "
                    size="small"
                  >
                    {{ template.dimension_status }}
                  </el-tag>
                </span>
              </div>
              <div class="info-row">
                <span class="info-label">最大分值:</span>
                <span class="info-value">{{ template.max_score }} 分</span>
              </div>
              <div class="info-row">
                <span class="info-label">权重系数:</span>
                <span class="info-value">{{ template.weight }}</span>
              </div>
              <div class="info-row">
                <span class="info-label">显示顺序:</span>
                <span class="info-value">第 {{ template.sort_order }} 位</span>
              </div>
              <div class="info-row">
                <span class="info-label">使用资产:</span>
                <span class="info-value">
                  {{
                    template.asset_count
                      ? `约 ${template.asset_count} 个资产使用此模板评分`
                      : "模板已禁用，暂无资产使用"
                  }}
                </span>
              </div>
            </div>
          </div>

          <div v-if="(usageData.templates || []).length === 0" class="no-data">
            暂无使用该维度的模板
          </div>
        </div>
      </el-card>

      <!-- 操作建议 -->
      <el-card class="usage-section-card">
        <template #header>
          <span class="section-title">操作建议</span>
        </template>
        <div class="suggestions">
          <div class="suggestion-item warning">
            <span class="suggestion-icon">⚠️</span>
            <div class="suggestion-content">
              <div class="suggestion-title">影响分析:</div>
              <div class="suggestion-text">
                <p>如果删除任一使用该代码的维度:</p>
                <ul>
                  <li>• 对应模板的评分规则将失效</li>
                  <li>• 使用该模板的资产评分将重新计算</li>
                  <li>• 历史评分记录中的该维度数据将保留但无法重现</li>
                </ul>
              </div>
            </div>
          </div>

          <div class="suggestion-item hint">
            <span class="suggestion-icon">💡</span>
            <div class="suggestion-content">
              <div class="suggestion-title">建议:</div>
              <div class="suggestion-text">
                <ul>
                  <li>• 如需删除，建议先禁用维度观察影响</li>
                  <li>• 确保有备用评分方案后再执行删除操作</li>
                  <li>• 考虑导出历史数据以备后续分析</li>
                </ul>
              </div>
            </div>
          </div>
        </div>
      </el-card>
    </div>

    <template #footer>
      <el-button @click="handleClose">关闭</el-button>
      <el-button type="default" @click="handleExportReport">导出报告</el-button>
      <el-button type="primary" @click="handleBatchEdit">批量编辑</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits, watch, onMounted } from "vue";
import { ElMessage } from "element-plus";
// import { getDimensionUsage } from "@/api/dimensionConfig";

// 定义属性和事件
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  dimensionCode: {
    type: String,
    default: "",
  },
});

const emit = defineEmits(["update:modelValue"]);

// 响应式数据
const visible = ref(props.modelValue);
const loading = ref(false);
const usageData = ref({
  total: 0,
  templates: [],
});

// 监听显示状态变化
watch(
  () => props.modelValue,
  (val) => {
    visible.value = val;
    if (val && props.dimensionCode) {
      fetchUsageData();
    }
  }
);

// 监听维度代码变化
watch(
  () => props.dimensionCode,
  (code) => {
    if (code && visible.value) {
      fetchUsageData();
    }
  }
);

// 页面初始化
onMounted(() => {
  if (props.dimensionCode && visible.value) {
    fetchUsageData();
  }
});

// 获取使用情况数据
const fetchUsageData = async () => {
  // loading.value = true;
  // try {
  //   const { data } = await getDimensionUsage({ code: props.dimensionCode });
  //   usageData.value = data;
  // } catch (error) {
  //   ElMessage.error("获取维度使用情况失败");
  // } finally {
  //   loading.value = false;
  // }
};

// 前往模板
const handleGoToTemplate = (templateId) => {
  // 实际应用中可以跳转到模板详情页
  ElMessage.info(`将跳转到模板ID: ${templateId}`);
  // router.push(`/template/${templateId}`);
};

// 导出报告
const handleExportReport = () => {
  // 实际应用中可以实现导出功能
  ElMessage.success("报告导出成功");
};

// 批量编辑
const handleBatchEdit = () => {
  // 实际应用中可以打开批量编辑对话框
  ElMessage.info("批量编辑功能待实现");
};

// 关闭
const handleClose = () => {
  emit("update:modelValue", false);
};

// 关闭前处理
const handleBeforeClose = () => {
  emit("update:modelValue", false);
  return true;
};
</script>

<style scoped lang="scss">
.usage-container {
  max-height: 60vh;
  overflow-y: auto;
  padding-right: 10px;
}

.usage-section-card {
  margin-bottom: 16px;
  border-radius: 6px;
  border: 1px solid #f0f0f0;
}

.section-title {
  font-weight: 500;
  color: #1d2129;
}

.monospace-text {
  font-family: monospace;
  background-color: #f5f5f5;
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 13px;
}

::v-deep .el-descriptions {
  margin-top: 10px;

  ::v-deep .el-descriptions__item {
    padding: 10px 16px;
  }

  ::v-deep .el-descriptions__label {
    width: 120px;
    font-weight: 500;
  }
}

.usage-details {
  padding: 5px 0;
}

.detail-title {
  font-weight: 500;
  margin-bottom: 15px;
  color: #1d2129;
}

.template-card {
  border: 1px solid #f0f0f0;
  border-radius: 6px;
  margin-bottom: 15px;
  overflow: hidden;
  transition: box-shadow 0.2s;

  &:hover {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  }
}

.template-header {
  display: flex;
  align-items: center;
  padding: 10px 15px;
  background-color: #f5f7fa;
}

.status-tag {
  margin-right: 10px;
}

.template-name {
  flex: 1;
  font-weight: 500;
}

.view-template-btn {
  padding: 0;
  font-size: 12px;
}

.template-divider {
  height: 1px;
  background-color: #f0f0f0;
}

.template-info {
  padding: 12px 15px;
}

.info-row {
  display: flex;
  margin-bottom: 8px;

  &:last-child {
    margin-bottom: 0;
  }
}

.info-label {
  display: inline-block;
  width: 100px;
  color: #86909c;
  font-size: 13px;
}

.info-value {
  flex: 1;
  font-size: 13px;
}

.no-data {
  padding: 30px;
  text-align: center;
  color: #86909c;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.suggestions {
  padding: 5px 0;
}

.suggestion-item {
  display: flex;
  margin-bottom: 15px;

  &:last-child {
    margin-bottom: 0;
  }
}

.suggestion-icon {
  font-size: 18px;
  margin-right: 10px;
  margin-top: 2px;
}

.suggestion-content {
  flex: 1;
}

.suggestion-title {
  font-weight: 500;
  margin-bottom: 5px;
  color: #1d2129;
}

.suggestion-text {
  font-size: 13px;
  line-height: 1.6;

  ul {
    padding-left: 20px;
    margin: 5px 0;
  }

  li {
    margin-bottom: 3px;
  }
}

.suggestion-item.warning {
  color: #fa8c16;
}

.suggestion-item.hint {
  color: #1890ff;
}
</style>
